﻿
using UnityEngine;
using System.Collections;
using AssetBundleHelper;
public class UpdateControl : MonoBehaviour {
	public MyOBJ myobj;
	[Header("勾选的话每次都会重新下载")]
	public bool clearKey;
	string version;
	bool finish;
	[Header("测试资源加载")]
	public bool TestResourcesLoad;
	// Use this for initialization
	void Start () {
		myobj.gameObject.SetActive (true);
		myobj.SetActive("ui",true);
		if (clearKey)
			TempUnit.myVersion = "";
		var abupdate = new GameObject ("ABUpdate").AddComponent<ABUpdate> ();
		abupdate.findNewVersionCallback = FindNewVersionCallback;
		abupdate.downLoadCallback = DownLoadCallback;
		abupdate.decompressionCallback = DecompressionCallback;
		abupdate.updateFinish = UpdateFinish;
		abupdate.Begin ();
	}
	void FindNewVersionCallback(string arg1, string arg2){
		version = arg2;
	}
	void DownLoadCallback(WWW obj){
		var size = ABUnit.CountSize(0);
		var info = size + obj.progress;
		SetInfo ( "版本号:"+version+"  下载进度:"+info);
		SetSlider (obj.progress);
	}
	void DecompressionCallback(string allsize,string nowsize,float precent){
		if (finish)
			return;
		SetInfo ( "版本号:"+version+"  解压中(不消耗流量):"+nowsize+"/"+allsize);
		SetSlider (precent);
	}

	void SetInfo(string info){
		myobj.SetLabel ("info", info);
	}
	void SetSlider(float value){
		myobj.SetSlider("slider",value);
	}
	void UpdateFinish(){
		if (finish == false) {
			finish = true;
			SetInfo ("更新完毕,祝您玩得开心");
			LoadMainAB ();
		}

	}
	void LoadMainAB(){
		GameTool.LoadMainAB (delegate(){
			myobj.gameObject.SetActive (false);
			if(TestResourcesLoad){
				GameTool.LoadJSON("UIFSM",delegate(ABObject obj) {
					Debug.Log(obj.jsonNode.ToString());
				});

//				GameTool.LuaLoadObject("mahjong",null,delegate(ABObject obj) {
//					
//				},"Prefab/Mahjong/",0);

				HotUI.MyLuaBehaviour.RunLua("lua_ui_testload");

				GameTool.OpenStateUI("login","");

				GameTool.LoadScene("scenelogin");

				GameSetting.Instance.AAA = true;
			
			}else{
				gameObject.SendMessage("OnUpdateFinish");
			}
		});
	}
}
